﻿using System;
using System.Windows;
using System.Windows.Input;

using ABCat.Shared;

namespace ABCat.UI.WPF.UI
{
	/// <summary>
	/// Interaction logic for LoginWindow.xaml
	/// </summary>
	public partial class LoginWindow
	{
		#region + Fields +

		private LoginInfo _realLoginInfo;

		#endregion
		#region + Properties +

		public LoginInfo LoginInfo
		{
			get { return (LoginInfo)DataContext; }
			set
			{
				_realLoginInfo = value;
				DataContext = value == null ? null : value.Clone();
			}
		}

		#endregion
		#region + Ctor +

		public LoginWindow()
		{
			InitializeComponent();
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			base.OnKeyDown(e);

			if (e.Key == Key.Enter)
			{
				Ok();
			}
		}

		#endregion
		#region + Logic +

		private void CancelBtn_OnClick(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
			Close();
		}

		private void OkBtn_Click(object sender, RoutedEventArgs e)
		{
			Ok();
		}

		private void Ok()
		{
			_realLoginInfo = LoginInfo == null ? null : (LoginInfo)LoginInfo.Clone();
			DataContext = _realLoginInfo;
			DialogResult = true;
			Close();
		}

		private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
		{
			LoginInfo.SetPassword(PasswordBox.Password);
		}

		#endregion
	}
}